package com.example.remotecontrol.data.repository

import android.content.Context
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject
import javax.inject.Singleton

// 创建一个DataStore实例
private val Context.settingsDataStore by preferencesDataStore(name = "settings")

/**
 * 设置仓库
 * 负责应用设置的存储和检索
 */
@Singleton
class SettingsRepository @Inject constructor(
    @ApplicationContext private val context: Context
) {
    private val dataStore = context.settingsDataStore
    
    // 设置键
    private object PreferencesKeys {
        val THEME_MODE = stringPreferencesKey("theme_mode")
        val VIBRATION_ENABLED = booleanPreferencesKey("vibration_enabled")
        val SOUND_EFFECTS_ENABLED = booleanPreferencesKey("sound_effects_enabled")
        val AUTO_SCAN_ENABLED = booleanPreferencesKey("auto_scan_enabled")
        val NOTIFICATIONS_ENABLED = booleanPreferencesKey("notifications_enabled")
    }
    
    // 主题模式选项
    enum class ThemeMode(val value: String) {
        SYSTEM("system"),
        LIGHT("light"),
        DARK("dark")
    }
    
    /**
     * 获取主题模式设置
     */
    val themeMode: Flow<ThemeMode> = dataStore.data.map { preferences ->
        val themeModeValue = preferences[PreferencesKeys.THEME_MODE] ?: ThemeMode.SYSTEM.value
        ThemeMode.values().find { it.value == themeModeValue } ?: ThemeMode.SYSTEM
    }
    
    /**
     * 设置主题模式
     */
    suspend fun setThemeMode(mode: ThemeMode) {
        dataStore.edit { preferences ->
            preferences[PreferencesKeys.THEME_MODE] = mode.value
        }
    }
    
    /**
     * 获取震动反馈设置
     */
    val vibrationEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
        preferences[PreferencesKeys.VIBRATION_ENABLED] ?: true
    }
    
    /**
     * 设置震动反馈
     */
    suspend fun setVibrationEnabled(enabled: Boolean) {
        dataStore.edit { preferences ->
            preferences[PreferencesKeys.VIBRATION_ENABLED] = enabled
        }
    }
    
    /**
     * 获取音效设置
     */
    val soundEffectsEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
        preferences[PreferencesKeys.SOUND_EFFECTS_ENABLED] ?: false
    }
    
    /**
     * 设置音效
     */
    suspend fun setSoundEffectsEnabled(enabled: Boolean) {
        dataStore.edit { preferences ->
            preferences[PreferencesKeys.SOUND_EFFECTS_ENABLED] = enabled
        }
    }
    
    /**
     * 获取自动扫描设置
     */
    val autoScanEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
        preferences[PreferencesKeys.AUTO_SCAN_ENABLED] ?: true
    }
    
    /**
     * 设置自动扫描
     */
    suspend fun setAutoScanEnabled(enabled: Boolean) {
        dataStore.edit { preferences ->
            preferences[PreferencesKeys.AUTO_SCAN_ENABLED] = enabled
        }
    }
    
    /**
     * 获取通知设置
     */
    val notificationsEnabled: Flow<Boolean> = dataStore.data.map { preferences ->
        preferences[PreferencesKeys.NOTIFICATIONS_ENABLED] ?: true
    }
    
    /**
     * 设置通知
     */
    suspend fun setNotificationsEnabled(enabled: Boolean) {
        dataStore.edit { preferences ->
            preferences[PreferencesKeys.NOTIFICATIONS_ENABLED] = enabled
        }
    }
    
    /**
     * 重置所有设置为默认值
     */
    suspend fun resetToDefaults() {
        dataStore.edit { preferences ->
            preferences[PreferencesKeys.THEME_MODE] = ThemeMode.SYSTEM.value
            preferences[PreferencesKeys.VIBRATION_ENABLED] = true
            preferences[PreferencesKeys.SOUND_EFFECTS_ENABLED] = false
            preferences[PreferencesKeys.AUTO_SCAN_ENABLED] = true
            preferences[PreferencesKeys.NOTIFICATIONS_ENABLED] = true
        }
    }
} 